<?php
$con=mysqli_connect("127.0.0.1","root","","morganse_crm");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

/*$result = mysqli_query($con,"CREATE TABLE IF NOT EXISTS `client_social_auth` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `provider` varchar(100) NOT NULL,
  `provider_uid` varchar(255) NOT NULL,
  `email` varchar(200) NOT NULL,
  `display_name` varchar(150) NOT NULL,
  `first_name` varchar(100) NOT NULL,
  `last_name` varchar(100) NOT NULL,
  `description` text,
  `profile_url` varchar(300) NOT NULL,
  `photo_url` varchar(300) DEFAULT NULL,
  `website_url` varchar(300) DEFAULT NULL,
  `gender` varchar(25) DEFAULT NULL,
  `language` varchar(200) DEFAULT NULL,
  `age` int(4) DEFAULT NULL,
  `birth_day` int(2) DEFAULT NULL,
  `birth_month` int(2) DEFAULT NULL,
  `birth_year` int(4) DEFAULT NULL,
  `email_verified` varchar(200) NOT NULL,
  `phone` varchar(25) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  `country` varchar(25) DEFAULT NULL,
  `region` varchar(50) DEFAULT NULL,
  `city` varchar(50) DEFAULT NULL,
  `zip` varchar(15) DEFAULT NULL, 
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;"); 

mysqli_query($con, "CREATE TABLE IF NOT EXISTS `client_social_application2contact` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `social_user_id` int(11) NOT NULL,
  `app_id` int(11) NOT NULL,
  `Contact_ID` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `social_user_id` (`social_user_id`,`app_id`,`Contact_ID`),
  KEY `Contact_ID` (`Contact_ID`),
  KEY `app_id` (`app_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");

mysqli_query($con, "ALTER TABLE `client_social_application2contact`
  ADD CONSTRAINT `client_social_application2contact_ibfk_1` FOREIGN KEY (`social_user_id`) REFERENCES `client_social_auth` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;");
exit;*/
//$result = mysqli_query($con,"SELECT * FROM client_social_authentications");

//mysqli_query($con,"TRUNCATE TABLE client_social_application2contact;");
//mysqli_query($con,"TRUNCATE TABLE client_social_auth;");


/*if(!mysqli_query($con, "INSERT INTO client_social_application2contact(`id`, `social_user_id`, `app_id`, `Contact_ID`) VALUES(NULL, '1', '176875', '177662');")) echo mysqli_error();*/


if(!$result = mysqli_query($con, "SELECT * FROM client_social_auth;")) 
	echo mysqli_error();
/*	
echo '<pre>';
while($row = mysqli_fetch_assoc($result)) {
print_r($row); 
}
*/


echo "<table border='1'>
<tr>
<th>id</th>
<th>provider</th>
<th>provider_uid</th>
<th>email</th>
<th>display_name</th>
<th>first_name</th>
<th>last_name</th>
<th>profile_url</th>
<th>website_url</th>
<th>created_at</th>
<th>gender</th>
</tr>";

while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['provider'] . "</td>";
  echo "<td>" . $row['provider_uid'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "<td>" . $row['display_name'] . "</td>";
  echo "<td>" . $row['first_name'] . "</td>";
  echo "<td>" . $row['last_name'] . "</td>";
  echo "<td>" . $row['profile_url'] . "</td>";
  echo "<td>" . $row['website_url'] . "</td>";
  echo "<td>" . $row['created_at'] . "</td>";
  echo "<td>" . $row['gender'] . "</td>";
  echo "</tr>";
}

echo "</table>";


echo '<br/>';
$result = mysqli_query($con,"SELECT * 
FROM  `client_social_application2contact`;");
echo '<pre>';

//$result = mysqli_query($con,"SELECT * FROM crm_contact WHERE Email = 'victor@maxfinance.com.au' LIMIT 1;");
echo "<table border='1'>
<tr>
<th>id</th>
<th>social_user_id</th>
<th>app_id</th>
<th>Contact_ID</th>

</tr>";

while($row = mysqli_fetch_assoc($result)) {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['social_user_id'] . "</td>";
  echo "<td>" . $row['app_id'] . "</td>";
  echo "<td>" . $row['Contact_ID'] . "</td>";
  echo "</tr>";
}

echo "</table>";
/*
$result = mysqli_query($con,"SELECT * 
FROM  `crm_contact` 
ORDER BY  `crm_contact`.`ContactID` DESC LIMIT 2;");
while($row = mysqli_fetch_array($result)){
print_r($row);
}
*/
/*echo '<br/>';
$app_id = 100;
$Contact_Id = 101;
$fbuser_contact_relation = "INSERT INTO client_social_applicationcontact(`id`, `social_user_id`, `app_id`, `Contact_ID`) VALUES('', '7', '".$app_id."', '".$Contact_Id."');"; 
			
if(!mysqli_query($con,$fbuser_contact_relation))
	echo mysqli_error();


$result = mysqli_query($con,"SELECT * 
FROM  `client_social_applicationcontact`;");
echo '<pre>';
while($row = mysqli_fetch_array($result)){
print_r($row);
}*/ 

//mysqli_query($con,"TRUNCATE TABLE client_social_applicationcontact;");
//if(!mysqli_query($con,"TRUNCATE TABLE client_social_authentications;")) echo mysqli_error();
mysqli_close($con);
?>